<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Base64转文件工具</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        h1 {
            color: #333;
            text-align: center;
        }

        .container {
            background-color: #f5f5f5;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        textarea {
            width: 100%;
            height: 200px;
            margin-bottom: 15px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            resize: vertical;
        }

        .controls {
            display: flex;
            gap: 10px;
            margin-bottom: 15px;
        }

        select,
        button {
            padding: 8px 15px;
            border-radius: 4px;
            border: 1px solid #ddd;
        }

        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }

        .preview {
            margin-top: 20px;
            text-align: center;
        }

        #filePreview {
            max-width: 100%;
            max-height: 300px;
            display: none;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>Base64转文件工具</h1>

        <div>
            <label for="base64Input">请输入Base64编码数据：</label>
            <textarea id="base64Input" placeholder="粘贴Base64编码数据..."></textarea>
        </div>

        <div class="controls">
            <select id="fileType">
                <option value="image">图片</option>
                <option value="audio">音频</option>
                <option value="video">视频</option>
            </select>

            <button id="previewBtn">预览</button>
            <button id="downloadBtn">下载文件</button>
        </div>

        <div class="preview">
            <div id="previewContainer">
                <p>预览区域</p>
            </div>
        </div>
    </div>

    <script>
        document.getElementById('previewBtn').addEventListener('click', previewFile);
        document.getElementById('downloadBtn').addEventListener('click', downloadFile);

        function previewFile() {
            const base64Data = document.getElementById('base64Input').value.trim();
            if (!base64Data) {
                alert('请输入Base64数据');
                return;
            }

            const fileType = document.getElementById('fileType').value;
            const previewContainer = document.getElementById('previewContainer');

            // 清空预览区域的HTML内容
            previewContainer.innerHTML = '';

            try {
                let mimeType;
                let previewElement;

                switch (fileType) {
                    case 'image':
                        mimeType = 'image/jpeg';
                        previewElement = document.createElement('img');
                        previewElement.id = 'filePreview';
                        previewElement.alt = '文件预览';
                        previewElement.style.maxWidth = '100%';
                        previewElement.style.maxHeight = '300px';
                        break;
                    case 'audio':
                        mimeType = 'audio/mpeg';
                        previewElement = document.createElement('audio');
                        previewElement.id = 'audioPreview';
                        previewElement.controls = true;
                        break;
                    case 'video':
                        mimeType = 'video/mp4';
                        previewElement = document.createElement('video');
                        previewElement.id = 'videoPreview';
                        previewElement.controls = true;
                        previewElement.style.maxWidth = '100%';
                        previewElement.style.maxHeight = '300px';
                        break;
                }

                previewElement.src = `data:${mimeType};base64,${base64Data}`;
                previewElement.style.display = 'block';
                previewContainer.appendChild(previewElement);

            } catch (error) {
                alert('预览失败: ' + error.message);
                console.error(error);
            }
        }

        function downloadFile() {
            const base64Data = document.getElementById('base64Input').value.trim();
            if (!base64Data) {
                alert('请输入Base64数据');
                return;
            }

            const fileType = document.getElementById('fileType').value;
            let extension, mimeType;

            switch (fileType) {
                case 'image':
                    extension = 'jpg';
                    mimeType = 'image/jpeg';
                    break;
                case 'audio':
                    extension = 'mp3';
                    mimeType = 'audio/mpeg';
                    break;
                case 'video':
                    extension = 'mp4';
                    mimeType = 'video/mp4';
                    break;
            }

            const filename = `downloaded_file.${extension}`;
            const link = document.createElement('a');
            link.href = `data:${mimeType};base64,${base64Data}`;
            link.download = filename;
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }
    </script>
</body>

</html>